package com.patent.quartz;

import com.patent.common.DateUtil;
import com.patent.common.JsonUtil;
import com.patent.common.NumberUtil;
import com.patent.common.SmsUtil;
import com.patent.domain.po.PaymentRecord;
import com.patent.domain.po.UnpaidFee;
import com.patent.domain.vo.PatentFeeVo;
import com.patent.service.MonitorPatentService;
import com.patent.service.PaymentRecordService;
import com.patent.service.UnpaidFeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangxiangwei6
 * @create 2018-07-22 13:15
 **/
public class PatentQuartzMonth {
	private static final Logger log = LoggerFactory.getLogger(PatentQuartz.class);

	@Autowired
	private UnpaidFeeService     unpaidFeeService;
	@Autowired
	private PaymentRecordService paymentRecordService;
	@Autowired
	private MonitorPatentService monitorPatentService;

	public void execute(){
		//查所有的手机号
		List<String> mobileList = monitorPatentService.queryAllMobile();
		if(mobileList != null && mobileList.size() > 0){
			for(int m = 0; m < mobileList.size(); m++){
				//查询所有的监控id
				Map<String,Object> queryMap = new HashMap<String,Object>();
				List<String> caseStatuList = new ArrayList<String>();
				caseStatuList.add("专利权维持");
				queryMap.put("caseStatuList",caseStatuList);
				queryMap.put("userMobile",mobileList.get(m));
				List<PatentFeeVo> monList = unpaidFeeService.selectAllIds(queryMap);
				if(monList != null && monList.size() > 0){
					List<PatentFeeVo> patentFeeVoList = unpaidFeeService.queryPatentFee(monList);
					if(patentFeeVoList != null && patentFeeVoList.size() >0){
						for(int i = 0; i < patentFeeVoList.size(); i++){
							PatentFeeVo patentFeeVo = patentFeeVoList.get(i);
							List<UnpaidFee> ufList = patentFeeVo.getUfList();
							if(ufList != null && ufList.size() > 0){
								patentFeeVo.setPatentId(ufList.get(0).getPatentId());
								patentFeeVo.setAmount(ufList.get(0).getAmount());
								patentFeeVo.setDueDate(ufList.get(0).getDueDate());
								patentFeeVo.setType(ufList.get(0).getType());
							}
							patentFeeVo.setUfList(null);
							Integer patentId = patentFeeVo.getPatentId();
							String newPatentType = patentFeeVo.getPatentType();
							Map<String,Object> map = new HashMap<String,Object>();
							map.put("patentId",patentId);
							PaymentRecord record = paymentRecordService.selectRecordByPatentId(patentId);  //查支付成功的最近一条记录
							if(record != null){
								String newType = record.getType();
								Integer year = NumberUtil.getNumFromString(newType);
								map.put("type","第"+(year + 1));
								if((newPatentType.contains("发明") && (year+1) > 20) || (newPatentType.contains("实用新型") && (year+1) > 10) ||
								   (newPatentType.contains("外观设计") && (year+1) > 10)){
									//最后一年已交完
									patentFeeVoList.remove(i);
									//patentFeeVoList.get(i).setShowFlag("0");
								}else{
									UnpaidFee unpaidFee = unpaidFeeService.queryUnpaidFee(map);
									if(unpaidFee != null){
										patentFeeVo.setType(unpaidFee.getType());
										patentFeeVo.setDueDate(unpaidFee.getDueDate());
										patentFeeVo.setAmount(unpaidFee.getAmount());
									}
								}
							}
						}
					}
					log.info("PatentQuartz list month:" + JsonUtil.write2JsonStr(patentFeeVoList));
					long nowTime = DateUtil.getNowDate().getTime(); //yyyy-MM-dd
					Map<String, Object> map = new HashMap<String, Object>();
					int count = 0;
					for(PatentFeeVo patentFeeVo : patentFeeVoList){
						//比较截止日期和今天相差的天数
						Date dueDate = patentFeeVo.getDueDate();
						if(dueDate != null){
							try {
								long dueTime = dueDate.getTime();
								int diffDay;
								if(nowTime < dueTime){
									diffDay = DateUtil.longOfTwoDate(DateUtil.getNowDate(), dueDate);
									if(diffDay <= 30){
										count++;
										patentFeeVo.setDiffDay(diffDay);
										//SmsUtil.sendRemindMessage(patentFeeVo);
									}
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					if(count > 0){
						SmsUtil.sendRemindMessageNew(mobileList.get(m), count, "月");
					}
				}
			}
		}
	}
}
